<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>

    </style>
</head>

<body>
    <div id="app">       
        <h1>热卖书籍</h1>
        <ul>
            <li v-for="(item,index) in bookobjs" v-bind:key="item.id" >
               书名：{{item.name}}
            </li>
        </ul>
        <input type="button" value="交换两本书" @click="exchangeBook">
    </div>

    <input type="button" value="给每个li增加自定义属性index" onclick="addIndex()" >
</body>

</html>
<script src="./js/vue2.js"></script>
<script>
    // 

    let vm = new Vue({
        el: "#app",
        data: {
            bookobjs:[
                {
                    "id":"01001",
                    "name":"大学"
                },
                {
                    "id":"01002",
                    "name":"中庸"
                },
                {
                    "id":"01003",
                    "name":"孟子"
                },
                {
                    "id":"01004",
                    "name":"论语"
                }
            ]
        },
        methods:{
            exchangeBook(){
                let firstBook = this.bookobjs.shift();
                let lastBook = this.bookobjs.pop();
                this.bookobjs.push(firstBook);
                this.bookobjs.unshift(lastBook);
            }
        }
    });

    function addIndex(){
        let liDoms = document.querySelectorAll("li");
        for(let i=0;i<liDoms.length;i++){
            liDoms[i].setAttribute("index",i)
        }
    }

</script>